package com.keyinong.restaurant.dapter;

import java.util.ArrayList;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import com.keyinong.restaurant.R;
import com.keyinong.restaurant.entity.OrderBean;
import com.keyinong.restaurant.entity.OrderBean.OrderInfo;
import com.keyinong.restaurant.tools.HttpTools;
import com.loopj.android.http.JsonHttpResponseHandler;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class PickupAdapter extends BaseAdapter {

	private Context mContext;
	private ArrayList<OrderInfo> mList;
	private String token;
	HttpTools httpTools=null;
	
	
	
	
	public PickupAdapter(Context mContext, ArrayList<OrderInfo> mList) {
		super();
		this.mContext = mContext;
		this.mList = mList;
		SharedPreferences sp = mContext.getSharedPreferences("spReg",
				Context.MODE_PRIVATE);
		token = sp.getString("token", "");
	}

	@Override
	public int getCount() {
		
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		
		final ViewHandle handle;
		if(view==null){
			handle=new ViewHandle();
			view=LayoutInflater.from(mContext).inflate
					(R.layout.item_order_layout,null);
			handle.tv_orderNo=(TextView) view.findViewById(R.id.tv_orderNo);
			handle.tv_user=(TextView) view.findViewById(R.id.tv_user);
			handle.tv_phone=(TextView) view.findViewById(R.id.tv_phone);
			handle.tv_time=(TextView) view.findViewById(R.id.tv_time);
			handle.btn_confirm=(Button) view.findViewById(R.id.btn_confirm);
			
			view.setTag(handle);
		}else{
			handle=(ViewHandle) view.getTag();
		}
		handle.tv_orderNo.setText(mList.get(position).getOrderno());
		handle.tv_user.setText(mList.get(position).getName());
		handle.tv_phone.setText(mList.get(position).getPhone());
		handle.tv_time.setText(mList.get(position).getOrder_time());
		handle.btn_confirm.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Printdialog(position,handle.btn_confirm);
				
			}
		});
		return view;
	}
	
	/**是否确认收货*/
	private void Printdialog(final int position,final Button btn_confirm) {

		final Dialog dialog = new Dialog(mContext);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

		View iscatview = LayoutInflater.from(mContext).inflate(
				R.layout.dialog_pay, null);
		TextView tital = (TextView) iscatview.findViewById(R.id.tv_dailogTital);
		TextView tv_no = (TextView) iscatview.findViewById(R.id.tv_no);
		TextView tv_yes = (TextView) iscatview.findViewById(R.id.tv_yes);
		tital.setText("是否确认收货?");
		dialog.setContentView(iscatview);
		dialog.show();
		tv_no.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		tv_yes.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				confirmOrder(position,btn_confirm);
				dialog.dismiss();
			}
		});
	}
	
	private void confirmOrder(final int position,final Button btn_confirm){
		if (httpTools == null) {
			httpTools = new HttpTools(mContext);
		}
		httpTools.finishOrder(token, mList.get(position).getId()+"",
				new JsonHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				try {
					boolean code = response.getBoolean("code");
					String msg = response.getString("msg");
//					Log.i("zq", "adapter---"+response.toString());
					if (code == true) {
						Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
					    mList.remove(position);
					    notifyDataSetChanged();
					} else {
						Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			@Override
			public void onFailure(int statusCode, Header[] headers,
				String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
			Toast.makeText(mContext, "确认收货失败", Toast.LENGTH_SHORT).show();
			}

		});
	}
	private class ViewHandle{
		private TextView tv_orderNo;
		private TextView tv_user;
		private TextView tv_phone;
		private TextView tv_time;
		private Button btn_confirm;
		
	}
	

}
